<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>python学习 | 云起</title><meta name="author" content="zjaaa"><meta name="copyright" content="zjaaa"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="python基础1.字面量 写在代码里固定的值   数字  字符串  列表 （list)  有序的可变序列    元组(tuple)  有序的不可变的python数据集合   集合(set)  无序不重复集合   字典（dictionary)  无序 key-value集合    123456789mylist&#x3D;[1,2,3]# 列表while进行循环index &#x3D;0while index&lt;">
<meta property="og:type" content="article">
<meta property="og:title" content="python学习">
<meta property="og:url" content="https://52zja.top/2024/02/08/python%E5%AD%A6%E4%B9%A0/index.html">
<meta property="og:site_name" content="云起">
<meta property="og:description" content="python基础1.字面量 写在代码里固定的值   数字  字符串  列表 （list)  有序的可变序列    元组(tuple)  有序的不可变的python数据集合   集合(set)  无序不重复集合   字典（dictionary)  无序 key-value集合    123456789mylist&#x3D;[1,2,3]# 列表while进行循环index &#x3D;0while index&lt;">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://52zja.top/img/icon.jpg">
<meta property="article:published_time" content="2024-02-08T04:11:12.000Z">
<meta property="article:modified_time" content="2024-02-29T11:21:25.137Z">
<meta property="article:author" content="zjaaa">
<meta property="article:tag" content="python">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://52zja.top/img/icon.jpg"><link rel="shortcut icon" href="/img/icon2.png"><link rel="canonical" href="https://52zja.top/2024/02/08/python%E5%AD%A6%E4%B9%A0/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css?v=4.13.0"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.5.1/css/all.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui@5.0.33/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlight.js","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: zjaaa","link":"链接: ","source":"来源: 云起","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'fancybox',
  Snackbar: undefined,
  infinitegrid: {
    js: 'https://cdn.jsdelivr.net/npm/@egjs/infinitegrid@4.11.1/dist/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'python学习',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2024-02-29 19:21:25'
}</script><script>(win=>{
      win.saveToLocal = {
        set: (key, value, ttl) => {
          if (ttl === 0) return
          const now = Date.now()
          const expiry = now + ttl * 86400000
          const item = {
            value,
            expiry
          }
          localStorage.setItem(key, JSON.stringify(item))
        },
      
        get: key => {
          const itemStr = localStorage.getItem(key)
      
          if (!itemStr) {
            return undefined
          }
          const item = JSON.parse(itemStr)
          const now = Date.now()
      
          if (now > item.expiry) {
            localStorage.removeItem(key)
            return undefined
          }
          return item.value
        }
      }
    
      win.getScript = (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        script.onerror = reject
        script.onload = script.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          script.onload = script.onreadystatechange = null
          resolve()
        }

        Object.keys(attr).forEach(key => {
          script.setAttribute(key, attr[key])
        })

        document.head.appendChild(script)
      })
    
      win.getCSS = (url, id = false) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onerror = reject
        link.onload = link.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          link.onload = link.onreadystatechange = null
          resolve()
        }
        document.head.appendChild(link)
      })
    
      win.activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
        if (t === 'dark') activateDarkMode()
        else if (t === 'light') activateLightMode()
      
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
      const detectApple = () => {
        if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
          document.documentElement.classList.add('apple')
        }
      }
      detectApple()
    })(window)</script><meta name="generator" content="Hexo 7.1.1"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "" data-lazy-src="/img/icon.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">10</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">19</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">3</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-sitemap"></i><span> 站点</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://aqcoder.cn"><i class="fa-fw fa fa-paper-plane"></i><span> 我的导航</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://bimg.cc"><i class="fa-fw iconfont icon-Bing"></i><span> 必应壁纸</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://status.aqcoder.cn"><i class="fa-fw fa fa-heartbeat"></i><span> 站点状态</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://next.aqcoder.cn"><i class="fa-fw fa fa-folder"></i><span> 旧博客</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-bookmark"></i><span> 博文</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/categories/"><i class="fa-fw fa fa-archive"></i><span> 分类</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/archives/"><i class="fa-fw fa fa-folder-open"></i><span> 归档</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 生活</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/share/"><i class="fa-fw fa fa-share-alt-square"></i><span> 分享</span></a></li><li><a class="site-page child" href="/music/"><i class="fa-fw fa fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/gallery/"><i class="fa-fw fa fa-images"></i><span> 图库</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/links/"><i class="fa-fw fa fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" target="_blank" rel="noopener" href="https://travellings.cn"><i class="fa-fw fa fa-train"></i><span> 开往</span></a></div><div class="menus_item"><a class="site-page" target="_blank" rel="noopener" href="https://foreverblog.cn/go.html"><i class="fa-fw fa fa-circle"></i><span> 虫洞</span></a></div><div class="menus_item"><a class="site-page" href="/comment/"><i class="fa-fw fa fa-paper-plane"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-user"></i><span> 关于我</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header"><nav id="nav"><span id="blog-info"><a href="/" title="云起"><span class="site-name">云起</span></a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-sitemap"></i><span> 站点</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://aqcoder.cn"><i class="fa-fw fa fa-paper-plane"></i><span> 我的导航</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://bimg.cc"><i class="fa-fw iconfont icon-Bing"></i><span> 必应壁纸</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://status.aqcoder.cn"><i class="fa-fw fa fa-heartbeat"></i><span> 站点状态</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://next.aqcoder.cn"><i class="fa-fw fa fa-folder"></i><span> 旧博客</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-bookmark"></i><span> 博文</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/categories/"><i class="fa-fw fa fa-archive"></i><span> 分类</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/archives/"><i class="fa-fw fa fa-folder-open"></i><span> 归档</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 生活</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/share/"><i class="fa-fw fa fa-share-alt-square"></i><span> 分享</span></a></li><li><a class="site-page child" href="/music/"><i class="fa-fw fa fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/gallery/"><i class="fa-fw fa fa-images"></i><span> 图库</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/links/"><i class="fa-fw fa fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" target="_blank" rel="noopener" href="https://travellings.cn"><i class="fa-fw fa fa-train"></i><span> 开往</span></a></div><div class="menus_item"><a class="site-page" target="_blank" rel="noopener" href="https://foreverblog.cn/go.html"><i class="fa-fw fa fa-circle"></i><span> 虫洞</span></a></div><div class="menus_item"><a class="site-page" href="/comment/"><i class="fa-fw fa fa-paper-plane"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-user"></i><span> 关于我</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">python学习</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2024-02-08T04:11:12.000Z" title="发表于 2024-02-08 12:11:12">2024-02-08</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2024-02-29T11:21:25.137Z" title="更新于 2024-02-29 19:21:25">2024-02-29</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">学习笔记</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="python学习"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="python基础"><a href="#python基础" class="headerlink" title="python基础"></a>python基础</h1><h2 id="1-字面量"><a href="#1-字面量" class="headerlink" title="1.字面量"></a>1.字面量</h2><blockquote>
<p>写在代码里固定的值</p>
</blockquote>
<ol>
<li><p>数字</p>
</li>
<li><p>字符串</p>
</li>
<li><p>列表 （list)</p>
<blockquote>
<p>有序的可变序列 </p>
</blockquote>
</li>
<li><p>元组(tuple)</p>
<blockquote>
<p>有序的不可变的python数据集合</p>
</blockquote>
</li>
<li><p>集合(set)</p>
<blockquote>
<p>无序不重复集合</p>
</blockquote>
</li>
<li><p>字典（dictionary)</p>
<blockquote>
<p>无序 key-value集合</p>
</blockquote>
</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">mylist=[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</span><br><span class="line"><span class="comment"># 列表while进行循环</span></span><br><span class="line">index =<span class="number">0</span></span><br><span class="line"><span class="keyword">while</span> index&lt;<span class="built_in">len</span>(mylist):</span><br><span class="line">    a = mylist[index]</span><br><span class="line">    index += <span class="number">1</span></span><br><span class="line"><span class="comment"># 列表for进行循环</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> mylist:</span><br><span class="line">    a = mylist[i]</span><br></pre></td></tr></table></figure>



<h2 id="2-注释"><a href="#2-注释" class="headerlink" title="2.注释"></a>2.注释</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># #号表示单行注释 一般#号后面空格在写注释</span></span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">三个双引号</span></span><br><span class="line"><span class="string">表示多行注释</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>

<h2 id="3-变量"><a href="#3-变量" class="headerlink" title="3.变量"></a>3.变量</h2><blockquote>
<p>能存储计算结果或表示值的抽象概念</p>
<p>变量没有类型  变量存储的数据是有类型的</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 定义一个变量 记录 钱包余额</span></span><br><span class="line">money = <span class="number">50</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 通过print语句，输出变量记录内容</span></span><br><span class="line"><span class="comment"># java用+ python用逗号会有空格 也可以用加号</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;钱包还有&quot;</span> , <span class="number">50</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 买了一个冰淇淋，花费十元</span></span><br><span class="line">money = money -<span class="number">10</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;买了冰淇淋花费10元，还剩余：&quot;</span>,money,<span class="string">&quot;元&quot;</span>) </span><br></pre></td></tr></table></figure>

<h3 id="查看数据类型"><a href="#查看数据类型" class="headerlink" title="查看数据类型"></a>查看数据类型</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">123</span></span><br><span class="line"><span class="comment"># 用变量接受123的类型 int</span></span><br><span class="line">int_type =<span class="built_in">type</span>(<span class="number">123</span>)</span><br></pre></td></tr></table></figure>

<h3 id="数据转换类型"><a href="#数据转换类型" class="headerlink" title="数据转换类型"></a>数据转换类型</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 将数字转换成字符串</span></span><br><span class="line">num_str = <span class="built_in">str</span>(<span class="number">11</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">type</span>(num_str),num_str)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 将字符串转换成数字</span></span><br><span class="line">num = <span class="built_in">int</span>(<span class="string">&quot;11&quot;</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">type</span>(num),num)</span><br><span class="line">nun = flat(<span class="string">&quot;11.8&quot;</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="4-标识符"><a href="#4-标识符" class="headerlink" title="4.标识符"></a>4.标识符</h2><blockquote>
<p>变量的名字 方法的名字 类的名字</p>
<p>用户在编程所使用的一系列的名字</p>
</blockquote>
<p>命名规则：</p>
<ol>
<li>只能用英文，数字，下划线，不能以数字开头！！</li>
<li>大小写敏感 （java也区分大小写）</li>
<li>不可使用关键字</li>
</ol>
<p>命名规范：</p>
<ol>
<li>python多个单词之间用下划线</li>
<li>java用驼峰</li>
</ol>
<h2 id="5-运算符"><a href="#5-运算符" class="headerlink" title="5.运算符"></a>5.运算符</h2><ol>
<li>整除为 &#x2F;&#x2F;</li>
<li>指数为 **</li>
<li>取模为 %&#x3D;</li>
</ol>
<h2 id="6-字符串的三种定义"><a href="#6-字符串的三种定义" class="headerlink" title="6.字符串的三种定义"></a>6.字符串的三种定义</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 单引号定义法</span></span><br><span class="line">name = <span class="string">&#x27;单引号定义&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 双引号定义法</span></span><br><span class="line">name = <span class="string">&quot;双引号定义&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 三引号定义法</span></span><br><span class="line"><span class="comment"># 不用变量接收的话 三个引号代表注释</span></span><br><span class="line">name = <span class="string">&quot;&quot;&quot;三引号定义&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>

<h2 id="7-字符串格式化"><a href="#7-字符串格式化" class="headerlink" title="7.字符串格式化"></a>7.字符串格式化</h2><blockquote>
<p>字符串拼接是不能直接拼接非字符串的变量，如数字</p>
<p>这时候就需要字符串格式化</p>
<ol>
<li>%s 字符串</li>
<li>%d 整数</li>
<li>%f  浮点数</li>
</ol>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">name = <span class="string">&quot;zjaaa&quot;</span></span><br><span class="line">message = <span class="string">&quot;学习Python的是： %s&quot;</span>  %name</span><br><span class="line"><span class="comment">#  如果有多个变量占位</span></span><br><span class="line"><span class="built_in">id</span> = <span class="number">1</span></span><br><span class="line">message = <span class="string">&quot;我是%s,我在学习目前排名第%s的python&quot;</span> %(name,<span class="built_in">id</span>)</span><br></pre></td></tr></table></figure>

<h3 id="可以用辅助符号m-n控制数据宽度和精度"><a href="#可以用辅助符号m-n控制数据宽度和精度" class="headerlink" title="可以用辅助符号m.n控制数据宽度和精度"></a>可以用辅助符号m.n控制数据宽度和精度</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 如果假如数字为11 因为宽度设成了5.所以得前面补三个空格</span></span><br><span class="line">%5d</span><br><span class="line"><span class="comment"># 表述宽度为7，精度为2 假设数字为11.345</span></span><br><span class="line">%<span class="number">7.2</span>f</span><br><span class="line"><span class="comment"># 格式化的数字为 【空格】【空格】11.35</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#也可以只设置精度 假如数字为11.342</span></span><br><span class="line">%<span class="number">.1</span>f</span><br><span class="line"><span class="comment"># 数字为11.3</span></span><br></pre></td></tr></table></figure>

<h3 id="字符串格式化方法2"><a href="#字符串格式化方法2" class="headerlink" title="字符串格式化方法2"></a>字符串格式化方法2</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">name = <span class="string">&quot;zjaaaa&quot;</span></span><br><span class="line">date = <span class="string">&quot;2024-02-08&quot;</span></span><br><span class="line"><span class="comment"># f:format 格式化标记</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">f&quot;我是<span class="subst">&#123;name&#125;</span>,今天是<span class="subst">&#123;date&#125;</span>,我正在学习python&quot;</span>)</span><br></pre></td></tr></table></figure>

<h3 id="表达式格式化"><a href="#表达式格式化" class="headerlink" title="表达式格式化"></a>表达式格式化</h3><blockquote>
<p>表达式: 1.有具体结果 2.代码片段</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">print</span>(<span class="string">&quot;1*1 的结果是: %d&quot;</span> %(<span class="number">1</span>*<span class="number">1</span>))</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">f&quot;1*2的结果是：<span class="subst">&#123;<span class="number">1</span>*<span class="number">2</span>&#125;</span>&quot;</span>)</span><br></pre></td></tr></table></figure>

<h2 id="8-数据输入"><a href="#8-数据输入" class="headerlink" title="8.数据输入"></a>8.数据输入</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">name = <span class="built_in">input</span>(<span class="string">&quot;请告诉我你是谁？&quot;</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;我知道了，你是: %s&quot;</span>%name)</span><br></pre></td></tr></table></figure>

<blockquote>
<p>input 默认接收到的是字符串 </p>
<p>想要别的类型记得转换一下！</p>
</blockquote>
<h2 id="9-布尔类型以及比较运算符"><a href="#9-布尔类型以及比较运算符" class="headerlink" title="9.布尔类型以及比较运算符"></a>9.布尔类型以及比较运算符</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># python的True以及False都要大写</span></span><br><span class="line"></span><br><span class="line"><span class="comment">## 比较运算符 </span></span><br><span class="line">num1 =<span class="number">10</span></span><br><span class="line">num2 =<span class="number">15</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">f&quot;num1 == num2 的结果为<span class="subst">&#123;num1 == num2&#125;</span>&quot;</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">f&quot;num1 &gt; num2的结果为<span class="subst">&#123;num1&gt;num2&#125;</span>&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># if 条件语句后面有冒号 </span></span><br><span class="line"><span class="comment"># 条件成立前面有四个空格</span></span><br><span class="line"><span class="comment"># else也要顶格写 后面有冒号！</span></span><br><span class="line"><span class="keyword">if</span> age &gt;= <span class="number">18</span>:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;我已经成年了&#x27;</span>)</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;我还没成年&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># elif为条件判断</span></span><br><span class="line">height =<span class="built_in">int</span>(<span class="built_in">input</span>(<span class="string">&quot;请输入你的身高：(cm)&quot;</span>))</span><br><span class="line">vip_level = <span class="built_in">int</span>(<span class="built_in">input</span>(<span class="string">&quot;请输入你的vip等级(1-5)&quot;</span>))</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> height&lt;<span class="number">120</span>:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;身高小于120cm，可以免费游玩&quot;</span>)</span><br><span class="line"><span class="keyword">elif</span> vip_lever &gt;<span class="number">3</span>:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;vip级别大于3，可以免费游玩&quot;</span>)</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;不好意思，条件都不满足，需要买票10元&#x27;</span>)</span><br><span class="line">    </span><br><span class="line"><span class="comment">#简单的一个猜数字游戏</span></span><br><span class="line">num = <span class="number">5</span></span><br><span class="line"><span class="keyword">if</span> <span class="built_in">int</span>(<span class="built_in">input</span>(<span class="string">&quot;请输入一个数字&quot;</span>)) ==num:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;恭喜你第一次就猜对了&quot;</span>)</span><br><span class="line"><span class="keyword">elif</span> <span class="built_in">int</span>(<span class="built_in">input</span>(<span class="string">&quot;猜错了，请再猜一次&quot;</span>)) ==num:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;猜对了&quot;</span>)</span><br><span class="line"><span class="keyword">elif</span> <span class="built_in">int</span>(<span class="built_in">input</span>(<span class="string">&quot;猜错了，最后再猜一次&quot;</span>)) ==num:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;恭喜你最后一次机会猜对了&quot;</span>)</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;sorry,三次机会都没有猜对&quot;</span>)</span><br></pre></td></tr></table></figure>

<h2 id="10-while循环"><a href="#10-while循环" class="headerlink" title="10. while循环"></a>10. while循环</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">i =<span class="number">0</span>;</span><br><span class="line"><span class="keyword">while</span> i&lt;<span class="number">100</span>:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;我在学习python第<span class="subst">&#123;i+<span class="number">1</span>&#125;</span>遍&quot;</span>)</span><br><span class="line">    i+=<span class="number">1</span></span><br></pre></td></tr></table></figure>

<h3 id="while嵌套循环"><a href="#while嵌套循环" class="headerlink" title="while嵌套循环"></a>while嵌套循环</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 给定一个条件 向小美表白100天 每天送十朵玫瑰花 </span></span><br><span class="line">i = <span class="number">1</span></span><br><span class="line"><span class="keyword">while</span> i&lt;=<span class="number">100</span>:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;今天是第%s天,准备表白...&quot;</span>%i)</span><br><span class="line">    j =<span class="number">1</span></span><br><span class="line">    <span class="keyword">while</span> j&lt;=<span class="number">10</span>:       </span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&#x27;送给小美第%s朵花&#x27;</span>%j)</span><br><span class="line">        j += <span class="number">1</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;小美，我喜欢你&#x27;</span>)</span><br><span class="line">    i+=<span class="number">1</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">f&quot;坚持到第<span class="subst">&#123;i-<span class="number">1</span>&#125;</span>天，表白成功！&quot;</span>)                   </span><br></pre></td></tr></table></figure>

<h2 id="11-for-循环"><a href="#11-for-循环" class="headerlink" title="11. for 循环"></a>11. for 循环</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">name =<span class="string">&#x27;zjaaa&#x27;</span></span><br><span class="line"><span class="keyword">for</span> x <span class="keyword">in</span> name:</span><br><span class="line">    <span class="built_in">print</span>(x)</span><br></pre></td></tr></table></figure>

<h3 id="range序列"><a href="#range序列" class="headerlink" title="range序列"></a>range序列</h3><blockquote>
<p>一般用于搭配for循环使用</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 语法1 range(num) </span></span><br><span class="line"><span class="built_in">range</span>(<span class="number">10</span>) <span class="comment">#num为10 取0-9 </span></span><br><span class="line"><span class="comment"># 语法2 range(num1,num2)</span></span><br><span class="line"><span class="built_in">range</span>(<span class="number">1</span>,<span class="number">100</span>) <span class="comment"># 取1-99</span></span><br><span class="line"><span class="comment"># 语法3 range(num1,num2,step)</span></span><br><span class="line"><span class="built_in">range</span>(<span class="number">1</span>,<span class="number">10</span>,<span class="number">2</span>) <span class="comment"># 取 1 3 5 7 9</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 搭配for循环使用</span></span><br><span class="line"><span class="keyword">for</span> x <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">10</span>):</span><br><span class="line">    <span class="built_in">print</span>(x)</span><br></pre></td></tr></table></figure>

<h3 id="continue"><a href="#continue" class="headerlink" title="continue"></a>continue</h3><p>跳过本次循环</p>
<h3 id="break"><a href="#break" class="headerlink" title="break"></a>break</h3><p>终止循环</p>
<h3 id="循环发工资小案例"><a href="#循环发工资小案例" class="headerlink" title="循环发工资小案例"></a>循环发工资小案例</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> random</span><br><span class="line"></span><br><span class="line">jiangjin=<span class="number">10000</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">1</span>,<span class="number">21</span>):</span><br><span class="line">    <span class="keyword">if</span> jiangjin == <span class="number">0</span>:</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">    jixiao=random.randint(<span class="number">1</span>,<span class="number">10</span>)</span><br><span class="line">    <span class="keyword">if</span> jixiao&lt;<span class="number">5</span>:</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&quot;员工i，绩效分<span class="subst">&#123;jixiao&#125;</span>,不发工资，下一位&quot;</span>)</span><br><span class="line">        <span class="keyword">continue</span></span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        jiangjin=jiangjin-<span class="number">1000</span></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&quot;向员工<span class="subst">&#123;i&#125;</span>发放工资1000元,奖金还剩<span class="subst">&#123;jiangjin&#125;</span>元&quot;</span>)</span><br></pre></td></tr></table></figure>

<h2 id="12-函数"><a href="#12-函数" class="headerlink" title="12.函数"></a>12.函数</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#函数定义</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">def 函数名(传入参数):</span></span><br><span class="line"><span class="string">    函数体</span></span><br><span class="line"><span class="string">    return 返回值</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="comment"># 如果函数没返回值则返回none none为假</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">add</span>(<span class="params">x,y</span>):</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    Add two numbers</span></span><br><span class="line"><span class="string">    :param x: </span></span><br><span class="line"><span class="string">    :param y: </span></span><br><span class="line"><span class="string">    :return: </span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">    <span class="keyword">return</span> x+y</span><br></pre></td></tr></table></figure>

<h2 id="13-list列表"><a href="#13-list列表" class="headerlink" title="13. list列表"></a>13. list列表</h2><p>列表可以倒着索引 右边第一个索引为-1 </p>
<ol>
<li>可以容纳多个元素</li>
<li>数据是有序存储的</li>
<li>允许重复数据存在</li>
<li>可以修改</li>
<li>可以容纳不同类型元素</li>
</ol>
<h3 id="list常用方法"><a href="#list常用方法" class="headerlink" title="list常用方法"></a>list常用方法</h3><blockquote>
<p>定义在class里面的称为方法   定义在外面的称为函数</p>
</blockquote>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">mylist = [<span class="string">&quot;java&quot;</span>,<span class="string">&quot;python&quot;</span>,<span class="string">&quot;c&quot;</span>]</span><br><span class="line">mylist = <span class="built_in">list</span></span><br><span class="line"><span class="comment"># 查找元素在列表内的下标索引</span></span><br><span class="line">index =mylist.index(<span class="string">&quot;java&quot;</span>) <span class="comment"># index==0</span></span><br><span class="line"><span class="comment"># 修改下标索引值</span></span><br><span class="line">mylist[<span class="number">2</span>] = <span class="string">&quot;php&quot;</span></span><br><span class="line"><span class="comment"># 在指定位置插入新元素</span></span><br><span class="line">mylist.insert(<span class="number">1</span>,<span class="string">&quot;best&quot;</span>)</span><br><span class="line"><span class="comment"># 在尾部添加元素</span></span><br><span class="line">mylist.append(<span class="string">&quot;c#&quot;</span>)</span><br><span class="line"><span class="comment"># 在列表尾部添加一批新元素</span></span><br><span class="line">mylist2 = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</span><br><span class="line">mylist.extend(mylist2)</span><br><span class="line"><span class="comment"># 删除指定索引的元素 (第一种)</span></span><br><span class="line">mylist.<span class="keyword">del</span>(<span class="number">2</span>)</span><br><span class="line"><span class="comment"># 删除指定索引的元素， 这种能接受到返回值</span></span><br><span class="line">element =mylist.pop(<span class="number">2</span>)</span><br><span class="line"><span class="comment"># 删除某元素在列表的第一个匹配项</span></span><br><span class="line">mylist.remove(<span class="string">&quot;java&quot;</span>)</span><br><span class="line"><span class="comment"># 清空列表</span></span><br><span class="line">mylist.clear()</span><br><span class="line"><span class="comment"># 统计列表某元素的数量</span></span><br><span class="line">count = mylist.count(<span class="string">&quot;java&quot;</span>)</span><br><span class="line"><span class="comment"># 统计列表的全部元素数量</span></span><br><span class="line">count = <span class="built_in">len</span>(mylist)</span><br></pre></td></tr></table></figure>

<h2 id="14-元组"><a href="#14-元组" class="headerlink" title="14.元组"></a>14.元组</h2><blockquote>
<p>有序，任意数量元素，允许重复元数，不可修改</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 定义元组</span></span><br><span class="line">mytuple = <span class="built_in">tuple</span></span><br><span class="line">mytuple = ()</span><br><span class="line">mytuple=(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">#定义单个元素的元组 需要有逗号！！</span></span><br><span class="line">mytuple=(<span class="number">1</span>,)</span><br><span class="line"><span class="comment"># 元组的嵌套</span></span><br><span class="line">mutuple = ((<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>),(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>))</span><br><span class="line"></span><br><span class="line"><span class="comment">#元组的操作</span></span><br><span class="line">mytuple = (<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>)</span><br><span class="line"><span class="comment"># 查找元素索引</span></span><br><span class="line">index = mytuple.indxe(<span class="number">1</span>)</span><br><span class="line"><span class="comment"># 查找某个元素在元组里的数量</span></span><br><span class="line">num = mutuple.count(<span class="number">2</span>)</span><br><span class="line"><span class="comment"># 统计元组元素的数量</span></span><br><span class="line">num =<span class="built_in">len</span>(mytuple)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 遍历元组</span></span><br><span class="line"><span class="comment"># while循环</span></span><br><span class="line">index = <span class="number">0</span></span><br><span class="line"><span class="keyword">while</span> index &lt; <span class="built_in">len</span>(mytuple):</span><br><span class="line">    a = mytuple[index]</span><br><span class="line"><span class="comment"># for循环</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> mytuple:</span><br><span class="line">    a = mytuple[i]</span><br></pre></td></tr></table></figure>

<h2 id="15-字符串"><a href="#15-字符串" class="headerlink" title="15.字符串"></a>15.字符串</h2><blockquote>
<p>只可以存储字符串 长度任意 支持下标索引 不可以修改</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">my_str =<span class="string">&quot;hello world&quot;</span></span><br><span class="line"><span class="comment"># 通过下标索引取值</span></span><br><span class="line">value = my_str[<span class="number">0</span>]</span><br><span class="line">value = my_str[-<span class="number">1</span>]</span><br><span class="line"><span class="comment"># 查找元素索引</span></span><br><span class="line">my_str.index(<span class="string">&quot;world&quot;</span>)</span><br><span class="line"><span class="comment"># 字符串的替换 替换次数参数（不加则为全部替换</span></span><br><span class="line">new_my_str =my_str.replace(<span class="string">&quot;world&quot;</span>,<span class="string">&quot;zjaaa&quot;</span>，<span class="number">1</span>)</span><br><span class="line"><span class="comment"># split方法 有两个参数 第一个参数分隔符 第二个参数分割几次 -1 或者不填都默认分割到不能再分</span></span><br><span class="line">my_str = <span class="string">&quot;hello python java c++&quot;</span></span><br><span class="line">new_str_list = my_str.split(<span class="string">&quot; &quot;</span>,<span class="number">2</span>)</span><br><span class="line"><span class="comment"># strip方法</span></span><br><span class="line"><span class="comment"># 不传参数 去除首尾空格</span></span><br><span class="line">my_str = <span class="string">&quot; hello &quot;</span></span><br><span class="line">new_mystr = my_str.strip()</span><br><span class="line"><span class="comment"># 传参数 去除首位指定参数 传的参数 按每个字符进行比对</span></span><br><span class="line">my_str=<span class="string">&quot;12hello21&quot;</span></span><br><span class="line">new_mystr=my_str.strip(<span class="string">&quot;12&quot;</span>)</span><br><span class="line"><span class="comment"># 统计字符串在字符串的出现次数</span></span><br><span class="line">count =my_str.count(<span class="string">&quot;hello&quot;</span>)</span><br><span class="line"><span class="comment"># 统计字符串的长度</span></span><br><span class="line">num = <span class="built_in">len</span>(my_str)</span><br></pre></td></tr></table></figure>

<h2 id="16-序列切片"><a href="#16-序列切片" class="headerlink" title="16.序列切片"></a>16.序列切片</h2><p>列表，元组，字符串都是序列</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 语法 序列[起始索引：结束索引：步长] 步长为负数表示反向取</span></span><br><span class="line">my_list = [<span class="number">0</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>]</span><br><span class="line">result = my_list[<span class="number">1</span>:<span class="number">4</span>] <span class="comment"># 得到结果 [1,2,3]</span></span><br><span class="line">my_list[::-<span class="number">1</span>]<span class="comment"># 倒着取</span></span><br></pre></td></tr></table></figure>

<h2 id="17-set集合"><a href="#17-set集合" class="headerlink" title="17. set集合"></a>17. set集合</h2><blockquote>
<p>可以容纳多个数据</p>
<p>可以容纳不同类型数据</p>
<p>数据是无序存储的</p>
<p>不允许重复数据存在</p>
<p>可以修改</p>
<p>支持for循环</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 定义集合</span></span><br><span class="line">my_set = <span class="built_in">set</span>()</span><br><span class="line">my_set =&#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 集合方法</span></span><br><span class="line"><span class="comment"># 添加新元素</span></span><br><span class="line">my_set.add(<span class="string">&quot;python&quot;</span>)</span><br><span class="line"><span class="comment"># 移除新元素</span></span><br><span class="line">my_set.remove(<span class="string">&quot;python&quot;</span>)</span><br><span class="line"><span class="comment"># 随机删除一个元素并取出</span></span><br><span class="line">value = my_set.pop()</span><br><span class="line"><span class="comment"># 清空集合</span></span><br><span class="line">my_set.clear()</span><br><span class="line"><span class="comment"># 取两个集合的差集 调用者有的,被调用者没的 </span></span><br><span class="line">set1=&#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>&#125;</span><br><span class="line">set2=&#123;<span class="number">1</span>,<span class="number">5</span>,<span class="number">6</span>&#125;</span><br><span class="line">set3 = set1.difference(set2) <span class="comment"># set3结果为&#123;2,3&#125;</span></span><br><span class="line"><span class="comment"># 消除两个集合相同的元素</span></span><br><span class="line">set1 =&#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>&#125;</span><br><span class="line">set2=&#123;<span class="number">1</span>,<span class="number">5</span>,<span class="number">6</span>&#125;</span><br><span class="line">set1.difference_update(set2) <span class="comment"># set1结果 &#123;2,3&#125;</span></span><br><span class="line"><span class="comment"># 两个集合合并</span></span><br><span class="line">set3 =set1.union(set2) <span class="comment">#结果去重合并</span></span><br><span class="line"><span class="comment"># 统计集合元素数量</span></span><br><span class="line">num = <span class="built_in">len</span>(set1)</span><br><span class="line"><span class="comment"># 集合的遍历</span></span><br><span class="line"><span class="comment"># 集合不支持下标索引，不能用while循环</span></span><br><span class="line"><span class="comment"># 可以用for循环</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> set1:</span><br><span class="line">    a = i</span><br></pre></td></tr></table></figure>

<h2 id="18-字典"><a href="#18-字典" class="headerlink" title="18.字典"></a>18.字典</h2><blockquote>
<p>注意的是 字典是无序的 取出来想排序记得对keys做sort</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 定义字典</span></span><br><span class="line">my_dict= &#123;<span class="string">&quot;王丽&quot;</span>：<span class="number">99</span>,<span class="string">&quot;周杰&quot;</span>：<span class="number">88</span>，<span class="string">&quot;林俊&quot;</span>:<span class="number">77</span>&#125;</span><br><span class="line"><span class="comment"># 定义空字典</span></span><br><span class="line">my_dict =&#123;&#125;</span><br><span class="line">my_dict = <span class="built_in">dict</span>()</span><br><span class="line"><span class="comment"># 从字典中基于key获取value</span></span><br><span class="line">score = my_dict[<span class="string">&quot;王丽&quot;</span>]</span><br><span class="line"><span class="comment"># 新字典</span></span><br><span class="line">my_dict[<span class="string">&quot;张鑫&quot;</span>] =<span class="number">66</span></span><br><span class="line"><span class="comment"># 添加 或 更新元素</span></span><br><span class="line">my_dcit[<span class="string">&quot;周杰&quot;</span>] = <span class="number">33</span></span><br><span class="line"><span class="comment"># 删除元素</span></span><br><span class="line">score = my_dict.pop(<span class="string">&quot;周杰&quot;</span>)</span><br><span class="line"><span class="comment"># 清空元素</span></span><br><span class="line">my_dict.clear()</span><br><span class="line"><span class="comment"># 获取全部的key</span></span><br><span class="line">keys = my_dict.keys</span><br><span class="line"><span class="comment"># 遍历字典</span></span><br><span class="line"><span class="keyword">for</span> key <span class="keyword">in</span> keys:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;key为<span class="subst">&#123;key&#125;</span>&quot;</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;value为<span class="subst">&#123;value&#125;</span>&quot;</span>)</span><br><span class="line"><span class="comment"># for循环</span></span><br><span class="line"><span class="keyword">for</span> key <span class="keyword">in</span> my_dict:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;key为<span class="subst">&#123;key&#125;</span>&quot;</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;value为<span class="subst">&#123;value&#125;</span>&quot;</span>)</span><br><span class="line"><span class="comment"># 统计字典元素数量</span></span><br><span class="line">num =<span class="built_in">len</span>(my_dict)</span><br></pre></td></tr></table></figure>

<h2 id="19-容器通用方法"><a href="#19-容器通用方法" class="headerlink" title="19.容器通用方法"></a>19.容器通用方法</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># len元素个数</span></span><br><span class="line"><span class="built_in">len</span>(<span class="built_in">list</span>)</span><br><span class="line"><span class="comment"># max最大元素</span></span><br><span class="line"><span class="built_in">max</span>(<span class="built_in">list</span>)</span><br><span class="line"><span class="comment"># 容器转列表</span></span><br><span class="line"><span class="built_in">list</span>(mylist)</span><br><span class="line"><span class="comment"># 容器转元组</span></span><br><span class="line"><span class="built_in">tuple</span>(mytuple)</span><br><span class="line"><span class="comment"># 容器转字符串</span></span><br><span class="line"><span class="built_in">str</span>(mystr)</span><br><span class="line"><span class="comment"># 容易转集合</span></span><br><span class="line"><span class="built_in">set</span>(myset)</span><br><span class="line"><span class="comment"># 进行容器的排序</span></span><br><span class="line"><span class="comment"># 不加reverse升序排列</span></span><br><span class="line"><span class="comment"># 加reverse True表示降序排</span></span><br><span class="line">sorted_list = <span class="built_in">sorted</span>(mylist，reverse=<span class="literal">False</span>) </span><br><span class="line"><span class="comment"># mylist.sort(key=,reverse=)</span></span><br><span class="line"><span class="comment"># 有两个参数 key 要求传入函数 一般直接用lambda</span></span><br><span class="line"><span class="comment"># reverse True降序 False 升序</span></span><br><span class="line"><span class="comment"># 没有返回值 直接改变</span></span><br><span class="line">mylist,sort(key =<span class="keyword">lambda</span> element: element[<span class="number">1</span>],reverse = <span class="literal">True</span>)</span><br><span class="line"><span class="comment"># 如果要反转排序</span></span><br><span class="line"><span class="built_in">sorted</span>(mylist,reverse=<span class="literal">True</span>)</span><br></pre></td></tr></table></figure>

<h2 id="20-函数进阶"><a href="#20-函数进阶" class="headerlink" title="20.函数进阶"></a>20.函数进阶</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 函数返回多个返回值</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">test_return</span>():</span><br><span class="line">    <span class="keyword">return</span> <span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span></span><br><span class="line"></span><br><span class="line">x,y,z = test_reeturn()</span><br><span class="line"><span class="built_in">print</span>(x)</span><br><span class="line"><span class="built_in">print</span>(y)</span><br><span class="line"><span class="built_in">print</span>(z)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 函数传参方式</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">user_info</span>(<span class="params">name,age,gender</span>):</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;姓名是<span class="subst">&#123;name&#125;</span>,年龄是<span class="subst">&#123;age&#125;</span>,性别是<span class="subst">&#123;女&#125;</span>&quot;</span>)</span><br><span class="line"><span class="comment"># 位置参数</span></span><br><span class="line">user_info(<span class="string">&quot;小明&quot;</span>,<span class="number">20</span>,<span class="string">&quot;男&quot;</span>)</span><br><span class="line"><span class="comment"># 关键字参数</span></span><br><span class="line">user_info(name=<span class="string">&quot;小王&quot;</span>，geder=<span class="string">&quot;女”,age=&quot;</span><span class="number">18</span><span class="string">&quot;)</span></span><br><span class="line"><span class="string"># 缺省参数</span></span><br><span class="line"><span class="string">def user_info(name,age,gender=&quot;</span>女 <span class="string">&quot;):</span></span><br><span class="line"><span class="string">    print(f&quot;</span>姓名是&#123;name&#125;,年龄是&#123;age&#125;,性别是&#123;女&#125;<span class="string">&quot;)</span></span><br><span class="line"><span class="string">          </span></span><br><span class="line"><span class="string"># 可变参数</span></span><br><span class="line"><span class="string">def user_info(*args):</span></span><br><span class="line"><span class="string">          print(args)</span></span><br><span class="line"><span class="string">user_info(1,2,3)</span></span><br><span class="line"><span class="string">def user_info(**kwags):</span></span><br><span class="line"><span class="string">          print(kwags)</span></span><br><span class="line"><span class="string">user_info(name=&#x27;小王&#x27;,age=11,gender=&#x27;男孩&#x27;)</span></span><br><span class="line"><span class="string"># 函数作为参数传递</span></span><br><span class="line"><span class="string">def test_func(compute):</span></span><br><span class="line"><span class="string">          result= compute(1,2)</span></span><br><span class="line"><span class="string">          print(result)</span></span><br><span class="line"><span class="string"># lambda </span></span><br><span class="line"><span class="string">test_func(lambda x,y:x+y)          </span></span><br></pre></td></tr></table></figure>

<h2 id="21-文件"><a href="#21-文件" class="headerlink" title="21.文件"></a>21.文件</h2><h3 id="1-打开文件"><a href="#1-打开文件" class="headerlink" title="1.打开文件"></a>1.打开文件</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">open</span>(name,mode,encoding)</span><br><span class="line"><span class="comment"># name打开目标文件名的字符串</span></span><br><span class="line"><span class="comment"># mode设置打开文件的模式</span></span><br><span class="line"><span class="comment">#只读r </span></span><br><span class="line"><span class="comment">#写入w 文件存在 ，原有内容删除。更新文件内容 文件不存在 创造文件 </span></span><br><span class="line"><span class="comment">#追加a 文件存在，追加内容 文件不存在 创造文件</span></span><br><span class="line"><span class="comment"># encoding编码格式 这个是关键字传参 encoding=</span></span><br><span class="line"><span class="built_in">open</span>(<span class="string">&quot;D:/测试.txt&quot;</span>,<span class="string">&quot;r&quot;</span>,encoding=<span class="string">&quot;utf-8&quot;</span>)</span><br></pre></td></tr></table></figure>

<h3 id="2-读入文件"><a href="#2-读入文件" class="headerlink" title="2.读入文件"></a>2.读入文件</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">f = <span class="built_in">open</span>(<span class="string">&quot;D:/测试.txt&quot;</span>,<span class="string">&quot;r&quot;</span>,encoding=<span class="string">&quot;utf-8&quot;</span>)</span><br><span class="line">f.read(参数为字符数，不带参数则表示读全部)</span><br><span class="line"><span class="comment"># 读取全部行</span></span><br><span class="line">f.readlines()</span><br><span class="line"><span class="comment"># 读取一行</span></span><br><span class="line">f.readline()</span><br><span class="line"><span class="comment"># for循环读取文件行</span></span><br><span class="line"><span class="keyword">for</span> line <span class="keyword">in</span> f:</span><br><span class="line">    <span class="built_in">print</span>(line)</span><br><span class="line">f.close()</span><br><span class="line"><span class="comment"># with open语法操作文件 执行完代码块会自动调用close方法</span></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">&quot;D:/测试.txt&quot;</span>,<span class="string">&quot;r&quot;</span>,encoding=<span class="string">&quot;utf-8&quot;</span>) <span class="keyword">as</span> f:</span><br><span class="line">    <span class="keyword">for</span> line <span class="keyword">in</span> f:</span><br><span class="line">        <span class="built_in">print</span>(line)</span><br></pre></td></tr></table></figure>

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#打开文件 以读取模式打开</span></span><br><span class="line">f = <span class="built_in">open</span>(<span class="string">&quot;D:/word.txt&quot;</span>,<span class="string">&quot;r&quot;</span>,encoding=<span class="string">&quot;utf-8&quot;</span>)</span><br><span class="line">count =<span class="number">0</span></span><br><span class="line"><span class="keyword">for</span> line <span class="keyword">in</span> f:</span><br><span class="line">    <span class="comment">#去除开头和结尾的空格以及换行符</span></span><br><span class="line">    line =line.strip()</span><br><span class="line">    words = line.split(<span class="string">&quot; &quot;</span>)</span><br><span class="line">    <span class="keyword">for</span> word <span class="keyword">in</span> words:</span><br><span class="line">        <span class="keyword">if</span> word == <span class="string">&quot;hello&quot;</span>:</span><br><span class="line">            count+=<span class="number">1</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">f&quot;出现的次数为<span class="subst">&#123;count&#125;</span>&quot;</span>)</span><br><span class="line">f.close()</span><br></pre></td></tr></table></figure>

<h3 id="3-写入文件"><a href="#3-写入文件" class="headerlink" title="3.写入文件"></a>3.写入文件</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#写入文件是先写入内存 最后写入硬盘</span></span><br><span class="line"><span class="comment">#如果想及时进硬盘 应该用flush()方法</span></span><br><span class="line">f = <span class="built_in">open</span>(<span class="string">&quot;G:/test.txt&quot;</span>,<span class="string">&quot;w&quot;</span>,encoding=<span class="string">&quot;utf-8&quot;</span>)</span><br><span class="line">f.write(<span class="string">&quot;hello world&quot;</span>)</span><br><span class="line">f.flush()</span><br></pre></td></tr></table></figure>

<h2 id="22-异常"><a href="#22-异常" class="headerlink" title="22.异常"></a>22.异常</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 捕获异常</span></span><br><span class="line"><span class="comment"># 这里可能出现异常</span></span><br><span class="line"><span class="keyword">try</span>:</span><br><span class="line">    result=<span class="number">10</span> /<span class="number">0</span></span><br><span class="line"><span class="comment"># 出现异常以这种方式解决    </span></span><br><span class="line"><span class="keyword">except</span>:</span><br><span class="line">    result=<span class="number">10</span>/<span class="number">1</span></span><br><span class="line">    </span><br><span class="line"><span class="comment">## 捕获指定的异常</span></span><br><span class="line"><span class="keyword">try</span>:</span><br><span class="line">    <span class="built_in">print</span>(未命名的name)</span><br><span class="line"><span class="keyword">except</span> NameError <span class="keyword">as</span> e:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;出现了变量未定义的异常&quot;</span>)</span><br><span class="line">    </span><br><span class="line"><span class="comment"># 捕获所有异常</span></span><br><span class="line"><span class="keyword">try</span>:</span><br><span class="line">    f = <span class="built_in">open</span>(<span class="string">&quot;D:/123.txt&quot;</span>,<span class="string">&quot;r&quot;</span>)</span><br><span class="line"><span class="keyword">except</span> Exception <span class="keyword">as</span> e:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;出现异常了&quot;</span>)</span><br><span class="line"><span class="comment"># 没有异常做的代码块    </span></span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;这里是没有异常做的事&quot;</span>)</span><br><span class="line"><span class="comment"># 无论有没有异常都会执行    </span></span><br><span class="line"><span class="keyword">finally</span>：</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="23-模块"><a href="#23-模块" class="headerlink" title="23.模块"></a>23.模块</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 引入一个模块</span></span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"><span class="comment"># 引入模块里的一个功能</span></span><br><span class="line"><span class="comment"># 不需要time.sleep 只需要sleep</span></span><br><span class="line"><span class="keyword">from</span> time <span class="keyword">import</span> sleep</span><br><span class="line"><span class="comment"># 导入模块里的所有功能</span></span><br><span class="line"><span class="keyword">from</span> time <span class="keyword">import</span> * </span><br><span class="line"><span class="comment"># 给模块起别名</span></span><br><span class="line"><span class="keyword">from</span> time <span class="keyword">as</span> t</span><br><span class="line"><span class="keyword">from</span> time <span class="keyword">import</span> sleep <span class="keyword">as</span> t</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="自定义模块"><a href="#自定义模块" class="headerlink" title="自定义模块"></a>自定义模块</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 文件名 my_module.py</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">test</span>(<span class="params">a,b</span>):</span><br><span class="line">    <span class="built_in">print</span>(a,b)</span><br><span class="line"><span class="comment"># 测试代码写这里 模块导入不会触发</span></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:    </span><br><span class="line"></span><br><span class="line"><span class="comment">#设置导全部包的函数    </span></span><br><span class="line">__all__ = [<span class="string">&#x27;file_util&#x27;</span>] </span><br></pre></td></tr></table></figure>

<h2 id="24-python包"><a href="#24-python包" class="headerlink" title="24. python包"></a>24. python包</h2><blockquote>
<p>多个模块的文件夹</p>
<p>里面含下面这个文件</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">__init.py__</span><br></pre></td></tr></table></figure>

<h3 id="自己写一个python包"><a href="#自己写一个python包" class="headerlink" title="自己写一个python包"></a>自己写一个python包</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">创建一个python包</span><br><span class="line">my_utils</span><br><span class="line">里面含三个python文件</span><br><span class="line">__init__.py 包需要的必备py</span><br><span class="line">file_util.py</span><br><span class="line">str_util.py</span><br></pre></td></tr></table></figure>

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># __init__.py</span></span><br></pre></td></tr></table></figure>

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># file_util.py</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">Created on Mon Jul 17 14:</span></span><br><span class="line"><span class="string">用于 处理文件相关工具</span></span><br><span class="line"><span class="string">@author: zjaaa</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">read_file</span>(<span class="params">filename</span>):</span><br><span class="line">    f = <span class="literal">None</span></span><br><span class="line">    <span class="keyword">try</span>:</span><br><span class="line">        f = <span class="built_in">open</span>(filename, <span class="string">&quot;r&quot;</span>, encoding=<span class="string">&quot;utf-8&quot;</span>)</span><br><span class="line">        content = f.read()</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&#x27;程序输出如下:\n读取的文件为:<span class="subst">&#123;filename&#125;</span>\n读的内容为:\n<span class="subst">&#123;content&#125;</span>&#x27;</span>)</span><br><span class="line">    <span class="keyword">except</span> Exception <span class="keyword">as</span> e:</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&quot;程序出现异常了,原因为<span class="subst">&#123;e&#125;</span>&quot;</span>)</span><br><span class="line">    <span class="keyword">finally</span>:</span><br><span class="line">        <span class="keyword">if</span> <span class="literal">None</span>:</span><br><span class="line">            f.close()</span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">append_file</span>(<span class="params">filename, content</span>):</span><br><span class="line">    f = <span class="built_in">open</span>(filename, <span class="string">&quot;a&quot;</span>, encoding=<span class="string">&quot;utf-8&quot;</span>)</span><br><span class="line">    f.write(content)</span><br><span class="line">    f.write(<span class="string">&quot;\n&quot;</span>)</span><br><span class="line">    f.close()</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># str_util.py</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">Created on Mon Jul 22 14:</span></span><br><span class="line"><span class="string">@author: zjaaa</span></span><br><span class="line"><span class="string">用于 处理字符串的工具</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">str_reverse</span>(<span class="params"><span class="built_in">str</span></span>):</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    用于反转字符串</span></span><br><span class="line"><span class="string">    :param 输入一个字符串</span></span><br><span class="line"><span class="string">    :return:字符串反转的结果</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">str</span>[::-<span class="number">1</span>]</span><br><span class="line"><span class="keyword">def</span> <span class="title function_">substr</span>(<span class="params">s,x,y</span>):</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    用于截取字符串</span></span><br><span class="line"><span class="string">    :param s:输入的字符串</span></span><br><span class="line"><span class="string">    :param x:开头的索引</span></span><br><span class="line"><span class="string">    :param y:结尾的索引</span></span><br><span class="line"><span class="string">    :return:截取的字符</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">    <span class="keyword">return</span> s[x:y+<span class="number">1</span>]</span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    <span class="built_in">str</span> = <span class="string">&#x27;大家好我叫zaaa，我学习的是python包的学习&#x27;</span></span><br><span class="line">    <span class="built_in">print</span>(str_reverse(<span class="built_in">str</span>))</span><br><span class="line">    prin</span><br></pre></td></tr></table></figure>



<h2 id="25-json"><a href="#25-json" class="headerlink" title="25. json"></a>25. json</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> json</span><br><span class="line">data=[&#123;<span class="string">&quot;name&quot;</span>:<span class="string">&quot;张三&quot;</span>,<span class="string">&quot;age&quot;</span>:<span class="number">16</span>&#125;,&#123;<span class="string">&quot;name&quot;</span>:<span class="string">&quot;李四&quot;</span>,<span class="string">&quot;age&quot;</span>:<span class="number">20</span>&#125;]</span><br><span class="line"><span class="comment"># 将python数据转换成json数据</span></span><br><span class="line">data = json.dumps(data)</span><br><span class="line"><span class="comment"># 不想让json数据变成ascii码</span></span><br><span class="line">data = json.dumps(data,ensure_ascii=Fa)</span><br><span class="line"><span class="comment"># 将json数据转换成python数据</span></span><br><span class="line">data =json.loads(data)</span><br></pre></td></tr></table></figure>

<h1 id="python面向对象"><a href="#python面向对象" class="headerlink" title="python面向对象"></a>python面向对象</h1><h2 id="1-类"><a href="#1-类" class="headerlink" title="1.类"></a>1.类</h2><blockquote>
<p>类包含属性和行为 一般用于描述现实世界事务</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Student</span>:</span><br><span class="line">    name = <span class="literal">None</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">sayHi</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&quot;Hello大家好,我是<span class="subst">&#123;self.name&#125;</span>&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">sayHi2</span>(<span class="params">self,msg</span>):</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&quot;hello大家好，我是<span class="subst">&#123;self.name&#125;</span>,<span class="subst">&#123;msg&#125;</span>&quot;</span>)</span><br><span class="line"></span><br><span class="line">stu = Student()</span><br><span class="line">stu.name= <span class="string">&quot;小明&quot;</span></span><br><span class="line">stu.sayHi()</span><br><span class="line">stu.sayHi2(<span class="string">&quot;很高兴认识大家&quot;</span>)</span><br></pre></td></tr></table></figure>

<h2 id="2-构造方法"><a href="#2-构造方法" class="headerlink" title="2.构造方法"></a>2.构造方法</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Student</span>:</span><br><span class="line">    <span class="comment">## 可以省略定义成员变量  直接在init方法里定义</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self,name,age,tel</span>):</span><br><span class="line">        self.name = name</span><br><span class="line">        self.age = age</span><br><span class="line">        self.tel = tel</span><br><span class="line">stu = Student(<span class="string">&#x27;John&#x27;</span>,<span class="number">18</span>,<span class="number">13221213456</span>)</span><br><span class="line"><span class="built_in">print</span>(stu.name)</span><br><span class="line"><span class="built_in">print</span>(stu.age)</span><br><span class="line"><span class="built_in">print</span>(stu.tel)</span><br></pre></td></tr></table></figure>

<h2 id="3-魔术方法"><a href="#3-魔术方法" class="headerlink" title="3.魔术方法"></a>3.魔术方法</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">__方法__</span><br></pre></td></tr></table></figure>

<blockquote>
<p>如上面所示的都叫做魔术方法</p>
</blockquote>
<h3 id="常见方法"><a href="#常见方法" class="headerlink" title="常见方法"></a>常见方法</h3><p>重写字符串方法</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Student</span>:</span><br><span class="line">    <span class="comment"># 将返回象打印结果 默认为内存地址</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__str__</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;想打印打印什么，一般重写字符串&quot;</span></span><br><span class="line">  </span><br></pre></td></tr></table></figure>

<p>lt 小于 大于比较方法</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Student</span>:</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self,name,age</span>):</span><br><span class="line">        self.name = name</span><br><span class="line">        self.age = age</span><br><span class="line">        <span class="comment"># 返回值 true或false</span></span><br><span class="line">        <span class="comment"># lt 只能用于小于和大于 不能用于等于</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__lt__</span>(<span class="params">self,other</span>):</span><br><span class="line">        <span class="keyword">return</span> self.age &lt; other.age</span><br></pre></td></tr></table></figure>

<p>le 小于等于 大于等于 比较方法</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Stduent</span>:</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self,name,age</span>):</span><br><span class="line">        self.name = name</span><br><span class="line">        self.age= age</span><br><span class="line">        <span class="comment"># 用于小于等于 以及 大于等于</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__le__</span>(<span class="params">self,other</span>):</span><br><span class="line">        <span class="keyword">return</span> self.age&lt;= other.age</span><br></pre></td></tr></table></figure>

<p>eq 等于 比较方法</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Student</span>:</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self,name,age</span>):</span><br><span class="line">        self.name=name</span><br><span class="line">        self.age=age</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__eq__</span>(<span class="params">self,other</span>):</span><br><span class="line">        self.age == other.age</span><br></pre></td></tr></table></figure>

<h2 id="4-封装"><a href="#4-封装" class="headerlink" title="4.封装"></a>4.封装</h2><p>将现实世界的事务在类中描述为属性和方法即为封装</p>
<p>私有方法和成员都是以两个下划线开头</p>
<p>类对象无法访问私有成员</p>
<p>类的其他成员可以访问私有成员</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 私有成员变量和方法都以两个下划线开头__</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Phone</span>:</span><br><span class="line">    __current_voltage = <span class="literal">None</span></span><br><span class="line">    </span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__keep_single_core</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;让cpu单核运行&quot;</span>)</span><br><span class="line">        </span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">call_by_5g</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="keyword">if</span> self.current_vlotage &gt;=<span class="number">1</span>:</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;5g通话已开启&quot;</span>)</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            self.__keep_single_core()</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;电量不足，无法使用5g通话，已设置单核运行&quot;</span>)</span><br></pre></td></tr></table></figure>

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Phone</span>:</span><br><span class="line">    __is5g_enable = <span class="literal">False</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__check_5g</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="keyword">if</span> self.__is5g_enable:</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;5g开启&quot;</span>)</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;5g关闭，使用4g&quot;</span>)</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">call_by_5g</span>(<span class="params">self</span>):</span><br><span class="line">        self.__check_5g()</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;正在通话中&quot;</span>)</span><br><span class="line">p = Phone()</span><br><span class="line">p.call_by_5g()</span><br></pre></td></tr></table></figure>

<pre><code>## 5.继承
</code></pre>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 单继承</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">类名</span>(<span class="title class_ inherited__">父类名</span>):</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Phone</span>:</span><br><span class="line">    IMEI = <span class="literal">None</span></span><br><span class="line">    producer =<span class="string">&quot;apple&quot;</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">call_by_4g</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&#x27;4g通话&#x27;</span>)</span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Phone2022</span>(<span class="title class_ inherited__">Phone</span>):</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">call_by_5g</span>(<span class="params">self</span>):</span><br><span class="line">         <span class="built_in">print</span>(<span class="string">&#x27;2022新功能：5g通话&#x27;</span>)</span><br><span class="line"><span class="comment"># 多继承</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Myphone</span>(RemoteControl,Phone,NFCReader):</span><br><span class="line">    <span class="comment"># pass 表示 不写新方法新成员 </span></span><br><span class="line">    <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 多继承优先级 从左至右 优先级逐渐递减</span></span><br></pre></td></tr></table></figure>

<h3 id="复写-父类方法属性"><a href="#复写-父类方法属性" class="headerlink" title="复写 父类方法属性"></a>复写 父类方法属性</h3><p>直接在子类重写属性和方法就好了  </p>
<h4 id="使用被子类重写的父类方法和变量"><a href="#使用被子类重写的父类方法和变量" class="headerlink" title="使用被子类重写的父类方法和变量"></a>使用被子类重写的父类方法和变量</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">通过 父类名.成员方法(self)</span><br><span class="line"></span><br><span class="line">通过 <span class="built_in">super</span>().方法</span><br><span class="line"></span><br><span class="line">父类名.成员变量</span><br><span class="line"></span><br><span class="line"><span class="built_in">super</span>().成员变量</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="6-类型注解"><a href="#6-类型注解" class="headerlink" title="6.类型注解"></a>6.类型注解</h2><blockquote>
<p>标记变量类型</p>
<p>类型注解仅仅是提示性的 不会影响程序运行</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 基础数据类型注解</span></span><br><span class="line">var_1: <span class="built_in">int</span> =<span class="number">10</span></span><br><span class="line">var_2: <span class="built_in">str</span> =<span class="string">&quot;zjaaa&quot;</span></span><br><span class="line">var_3: <span class="built_in">bool</span> = <span class="literal">True</span></span><br><span class="line"><span class="comment"># 类对象类型注解</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Student</span>:</span><br><span class="line">    <span class="keyword">pass</span></span><br><span class="line">stu: Student = Student()</span><br><span class="line"><span class="comment"># 基础容器类型注解</span></span><br><span class="line">my_list: <span class="built_in">list</span>=[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</span><br><span class="line">my_tuple: <span class="built_in">tuple</span>=(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>)</span><br><span class="line">my_dict: <span class="built_in">dict</span>=&#123;<span class="string">&quot;zjaaa&quot;</span>：<span class="number">666</span>&#125;</span><br><span class="line"><span class="comment"># 容器类型详细注解</span></span><br><span class="line">my_list: <span class="built_in">list</span>[<span class="built_in">int</span>] =[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</span><br><span class="line">my_tuple: <span class="built_in">tuple</span>[<span class="built_in">int</span>,<span class="built_in">str</span>,<span class="built_in">bool</span>]=(<span class="number">1</span>,<span class="string">&quot;zjaaa&quot;</span>，<span class="literal">True</span>)</span><br><span class="line">my_dict: <span class="built_in">dict</span>[<span class="built_in">str</span>,<span class="built_in">int</span>] = &#123;<span class="string">&quot;itheima&quot;</span>:<span class="number">666</span>&#125;  </span><br><span class="line">    </span><br><span class="line"><span class="comment"># 在注释中进行注解</span></span><br><span class="line">var_1 = <span class="number">111</span> <span class="comment"># type: <span class="built_in">int</span></span></span><br><span class="line">var_2 = josn.loads(<span class="string">&#x27;&#123;&quot;name&quot;：&quot;zhangsan&quot;&#125;&#x27;</span>) <span class="comment"># type: <span class="built_in">dict</span>[<span class="built_in">str</span>,<span class="built_in">str</span>]</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 函数方法形参进行注解</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">add</span>(<span class="params">x: <span class="built_in">int</span>,y: <span class="built_in">int</span></span>):</span><br><span class="line">    <span class="keyword">return</span> x + y</span><br><span class="line"><span class="comment"># 对返回值进行类型注解</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">func</span>(<span class="params">data: <span class="built_in">list</span></span>) -&gt; <span class="built_in">list</span>:</span><br><span class="line">    <span class="keyword">return</span> data</span><br><span class="line"></span><br><span class="line"><span class="comment"># Union类型 定义联合类型注解</span></span><br><span class="line"><span class="keyword">from</span> typing <span class="keyword">import</span> <span class="type">Union</span></span><br><span class="line">my_list: <span class="built_in">list</span>[<span class="type">Union</span>[<span class="built_in">int</span>,<span class="built_in">str</span>]] = [<span class="number">1</span>,<span class="number">2</span>,<span class="string">&quot;zjaaa&quot;</span>]</span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">func</span>(<span class="params">data: <span class="type">Union</span>[<span class="built_in">int</span>,<span class="built_in">str</span>]</span>) -&gt; <span class="type">Union</span>[<span class="built_in">int</span>,<span class="built_in">str</span>]:</span><br><span class="line">    <span class="keyword">pass</span></span><br></pre></td></tr></table></figure>

<h2 id="6-多态"><a href="#6-多态" class="headerlink" title="6.多态"></a>6.多态</h2><blockquote>
<p>同一个行为 使用不同的对象 获取不同的状态</p>
<p>抽象类 为 包含抽象方法的类</p>
<p>抽象方法 为 没有具体实现的方法 （pass）</p>
<p>抽象类的作用</p>
<p>用于提供标准 便于子类做具体实现</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">AC</span>:</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">cool_wind</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="string">&quot;&quot;&quot;制冷&quot;&quot;&quot;</span></span><br><span class="line">        <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">hot_wind</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="string">&quot;&quot;&quot;制热&quot;&quot;&quot;</span></span><br><span class="line">        <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">swing_l_r</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="string">&quot;&quot;&quot;左右摆风&quot;&quot;&quot;</span></span><br><span class="line">        <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Midea_AC</span>(<span class="title class_ inherited__">AC</span>):</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">cool_wind</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;美的吹冷风&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">hot_wind</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&#x27;美的吹热风&#x27;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">swing_l_r</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;美的左右摇摆&quot;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">GREE_AC</span>(<span class="title class_ inherited__">AC</span>):</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">cool_wind</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;格力吹冷风&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">hot_wind</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;格力吹热风&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">swing_l_r</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&#x27;格力左右摇摆&#x27;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">make_cool</span>(<span class="params">ac: AC</span>):</span><br><span class="line">    ac.cool_wind()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">midea_ac = Midea_AC()</span><br><span class="line">gree_ac = GREE_AC()</span><br><span class="line">make_cool(midea_ac)</span><br><span class="line">make_cool(gree_ac)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="7-python使用mysql"><a href="#7-python使用mysql" class="headerlink" title="7. python使用mysql"></a>7. python使用mysql</h2><blockquote>
<p>pip install pymysql</p>
</blockquote>
<p>创建表</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> pymysql <span class="keyword">import</span> Connection</span><br><span class="line"><span class="comment"># 获取mysql的连接对象</span></span><br><span class="line">conn = Connection(</span><br><span class="line">    host= <span class="string">&quot;localhost&quot;</span>,</span><br><span class="line">    port=<span class="number">3306</span>,</span><br><span class="line">    user=<span class="string">&quot;root&quot;</span>,</span><br><span class="line">    password=<span class="string">&quot;root&quot;</span></span><br><span class="line">)</span><br><span class="line"><span class="comment"># 执行非查询sql</span></span><br><span class="line"><span class="comment"># 获取游标对象</span></span><br><span class="line">cursor = conn.cursor()</span><br><span class="line"><span class="comment"># 选择数据库</span></span><br><span class="line">conn.select_db(<span class="string">&quot;test&quot;</span>)</span><br><span class="line"><span class="comment"># 执行sql</span></span><br><span class="line">cursor.execute(<span class="string">&quot;create table test_pymysql(id int)&quot;</span>)</span><br><span class="line"><span class="comment"># 关闭连接</span></span><br><span class="line">conn.close()</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>查询数据</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> pymysql <span class="keyword">import</span> Connection</span><br><span class="line"><span class="comment"># 获取mysql的连接对象</span></span><br><span class="line">conn = Connection(</span><br><span class="line">    host= <span class="string">&quot;localhost&quot;</span>,</span><br><span class="line">    port=<span class="number">3306</span>,</span><br><span class="line">    user=<span class="string">&quot;root&quot;</span>,</span><br><span class="line">    password=<span class="string">&quot;root&quot;</span></span><br><span class="line">)</span><br><span class="line"><span class="comment"># 执行查询sql</span></span><br><span class="line"><span class="comment"># 获取游标对象</span></span><br><span class="line">cursor = conn.cursor()</span><br><span class="line"><span class="comment"># 选择数据库</span></span><br><span class="line">conn.select_db(<span class="string">&quot;test&quot;</span>)</span><br><span class="line"><span class="comment"># 执行sql</span></span><br><span class="line">cursor.execute(<span class="string">&quot;select * from dakatime&quot;</span>)</span><br><span class="line">rs = cursor.fetchall()</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> rs:</span><br><span class="line">    <span class="built_in">print</span>(i)</span><br><span class="line">conn.close()</span><br></pre></td></tr></table></figure>

<p>插入数据</p>
<p>发现一般默认建表引擎都不是innodb</p>
<p>如果事务没生效可以看看建表引擎 </p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> pymysql <span class="keyword">import</span> Connection</span><br><span class="line"><span class="comment"># 获取mysql的连接对象</span></span><br><span class="line">conn = Connection(</span><br><span class="line">    host= <span class="string">&quot;localhost&quot;</span>,</span><br><span class="line">    port=<span class="number">3306</span>,</span><br><span class="line">    user=<span class="string">&quot;root&quot;</span>,</span><br><span class="line">    password=<span class="string">&quot;root&quot;</span>,</span><br><span class="line">    <span class="comment"># 设置事务 一般会默认false </span></span><br><span class="line">    autocommit=<span class="literal">False</span></span><br><span class="line">)</span><br><span class="line"><span class="comment"># 执行插入sql</span></span><br><span class="line">cursor = conn.cursor()</span><br><span class="line"><span class="comment"># 选择数据库</span></span><br><span class="line">conn.select_db(<span class="string">&quot;test&quot;</span>)</span><br><span class="line"><span class="comment"># 执行sql</span></span><br><span class="line">cursor.execute(<span class="string">&quot;insert into test_pymysql values (3)&quot;</span>)</span><br><span class="line"><span class="comment">## 提交事务</span></span><br><span class="line">conn.commit()</span><br><span class="line">conn.close()</span><br></pre></td></tr></table></figure>

<h2 id="8-闭包"><a href="#8-闭包" class="headerlink" title="8.闭包"></a>8.闭包</h2><p>记得用nonlocal修改属性</p>
<h2 id="9-装饰器"><a href="#9-装饰器" class="headerlink" title="9.装饰器"></a>9.装饰器</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">outer</span>(<span class="params">func</span>):</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">inner</span>():</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&#x27;我睡觉了&#x27;</span>)</span><br><span class="line">        func()</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&#x27;我起床了&#x27;</span>)</span><br><span class="line">    <span class="keyword">return</span> inner</span><br><span class="line"><span class="meta">@outer </span><span class="comment"># 直接把这个函数当参数传入@后面的函数</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">sleep</span>():</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;睡眠中&#x27;</span>)</span><br><span class="line"><span class="comment"># 如果没写@增强方法 一般的执行操作</span></span><br><span class="line">fn = outer(sleep)</span><br><span class="line">fn()</span><br><span class="line"><span class="comment"># 写了@增强方法的执行操作</span></span><br><span class="line">fn()</span><br></pre></td></tr></table></figure>

<h2 id="10-多线程"><a href="#10-多线程" class="headerlink" title="10.多线程"></a>10.多线程</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> threading</span><br><span class="line"></span><br><span class="line"><span class="comment"># target 指定函数任务 args 指定元组传参 kwargs指定字典传参</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">sing</span>(<span class="params">msg</span>):</span><br><span class="line">    <span class="keyword">while</span>(<span class="literal">True</span>):</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&#x27;我在唱<span class="subst">&#123;msg&#125;</span>&#x27;</span>)</span><br><span class="line">sing_thread= threading.thread(target=sing,args=(<span class="string">&#x27;两只老虎&#x27;</span>,))</span><br><span class="line">s</span><br></pre></td></tr></table></figure>



<h1 id="第三方包的使用以及记录"><a href="#第三方包的使用以及记录" class="headerlink" title="第三方包的使用以及记录"></a>第三方包的使用以及记录</h1><h2 id="pyechars图例库的使用"><a href="#pyechars图例库的使用" class="headerlink" title="pyechars图例库的使用"></a>pyechars图例库的使用</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> pyecharts.charts <span class="keyword">import</span> Line</span><br><span class="line"><span class="keyword">from</span> pyecharts.options <span class="keyword">import</span> TitleOpts, LegendOpts, ToolboxOpts, VisualMapOpts</span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建一个折线图对象</span></span><br><span class="line">line = Line()</span><br><span class="line"><span class="comment"># 给折线图添加x坐标</span></span><br><span class="line">line.add_xaxis([<span class="string">&quot;中国&quot;</span>,<span class="string">&quot;美国&quot;</span>,<span class="string">&quot;英国&quot;</span>])</span><br><span class="line"><span class="comment"># 给折线图添加y坐标</span></span><br><span class="line">line.add_yaxis(<span class="string">&quot;GDP&quot;</span>,[<span class="number">30</span>,<span class="number">20</span>,<span class="number">10</span>])</span><br><span class="line"><span class="comment"># 设置全局配置项</span></span><br><span class="line">line.set_global_opts(</span><br><span class="line">    <span class="comment"># 展示标题 设置离左边居中 离下面百分之一距离</span></span><br><span class="line">    title_opts=TitleOpts(title=<span class="string">&quot;GDP展示&quot;</span>,pos_left=<span class="string">&quot;center&quot;</span>,pos_bottom=<span class="string">&quot;1%&quot;</span>),</span><br><span class="line">    <span class="comment"># 设置图例</span></span><br><span class="line">    legend_opts=LegendOpts(is_show=<span class="literal">True</span>),</span><br><span class="line">    <span class="comment"># 设置工具箱</span></span><br><span class="line">    toolbox_opts=ToolboxOpts(is_show=<span class="literal">True</span>),</span><br><span class="line">    <span class="comment"># 设置视觉映射</span></span><br><span class="line">    visualmap_opts=VisualMapOpts(is_show=<span class="literal">True</span>)</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">line.render()</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="1-折线图使用"><a href="#1-折线图使用" class="headerlink" title="1.折线图使用"></a>1.折线图使用</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> json</span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> pyecharts.charts <span class="keyword">import</span> Line</span><br><span class="line"><span class="keyword">from</span> pyecharts.options <span class="keyword">import</span> TitleOpts, ToolboxOpts, LabelOpts</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">get_country_trend_data</span>(<span class="params">file_str</span>):</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    获取国家的trend值</span></span><br><span class="line"><span class="string">    :param file_str:填入数据路径</span></span><br><span class="line"><span class="string">    :return: 返回trend值</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">    <span class="keyword">with</span> <span class="built_in">open</span>(file_str,<span class="string">&quot;r&quot;</span>,encoding=<span class="string">&#x27;utf-8&#x27;</span>) <span class="keyword">as</span> f:</span><br><span class="line">        country_data = f.read()</span><br><span class="line">        country_data = country_data.split(<span class="string">&#x27;(&#x27;</span>,<span class="number">1</span>)[<span class="number">1</span>]</span><br><span class="line">        country_data = country_data[:-<span class="number">2</span>]</span><br><span class="line">        country_dict = json.loads(country_data)</span><br><span class="line">        <span class="keyword">return</span> country_dict[<span class="string">&#x27;data&#x27;</span>][<span class="number">0</span>][<span class="string">&#x27;trend&#x27;</span>]</span><br><span class="line"><span class="keyword">def</span> <span class="title function_">get_country_x_data</span>(<span class="params">trend_value</span>):</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    获取天数</span></span><br><span class="line"><span class="string">    :param trend_value: 填入trend值</span></span><br><span class="line"><span class="string">    :return: 返回x坐标</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">    <span class="keyword">return</span> trend_value[<span class="string">&#x27;updateDate&#x27;</span>][:<span class="number">314</span>]</span><br><span class="line"><span class="keyword">def</span> <span class="title function_">get_country_y_data</span>(<span class="params">trend_value</span>):</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    获取每天感染人数</span></span><br><span class="line"><span class="string">    :param trend_value:  填入trend值</span></span><br><span class="line"><span class="string">    :return:  返回y坐标</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">    <span class="keyword">return</span> trend_value[<span class="string">&#x27;list&#x27;</span>][<span class="number">0</span>][<span class="string">&#x27;data&#x27;</span>][:<span class="number">314</span>]</span><br><span class="line"><span class="comment"># 获取三国 trend数据</span></span><br><span class="line">us_trend_data = get_country_trend_data(<span class="string">&#x27;G:/美国.txt&#x27;</span>)</span><br><span class="line">jp_trend_data = get_country_trend_data(<span class="string">&#x27;G:/日本.txt&#x27;</span>)</span><br><span class="line">in_trend_data = get_country_trend_data(<span class="string">&#x27;G:/印度.txt&#x27;</span>)</span><br><span class="line"><span class="comment"># 获取一年日期</span></span><br><span class="line">x_data = get_country_x_data(us_trend_data)</span><br><span class="line"><span class="comment"># 获取三国 感染人数</span></span><br><span class="line">us_y_data = get_country_y_data(us_trend_data)</span><br><span class="line">jp_y_data = get_country_y_data(jp_trend_data)</span><br><span class="line">in_y_data = get_country_y_data(in_trend_data)</span><br><span class="line"><span class="comment"># 生成分析图</span></span><br><span class="line">line = Line()</span><br><span class="line">line.add_xaxis(x_data)</span><br><span class="line">line.add_yaxis(<span class="string">&#x27;美国确诊人数&#x27;</span>,us_y_data,label_opts=LabelOpts(is_show=<span class="literal">False</span>))</span><br><span class="line">line.add_yaxis(<span class="string">&#x27;日本确诊人数&#x27;</span>,jp_y_data,label_opts=LabelOpts(is_show=<span class="literal">False</span>))</span><br><span class="line">line.add_yaxis(<span class="string">&#x27;印度确诊人数&#x27;</span>,in_y_data,label_opts=LabelOpts(is_show=<span class="literal">False</span>))</span><br><span class="line">line.set_global_opts(</span><br><span class="line">    title_opts=TitleOpts(title=<span class="string">&quot;国家感染分析图&quot;</span>),</span><br><span class="line">    toolbox_opts=ToolboxOpts(is_show=<span class="literal">True</span>)</span><br><span class="line">)</span><br><span class="line">line.render()</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h3 id="2-地图的使用"><a href="#2-地图的使用" class="headerlink" title="2.地图的使用"></a>2.地图的使用</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> json</span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> pyecharts.charts <span class="keyword">import</span> Map</span><br><span class="line"><span class="keyword">from</span> pyecharts.options <span class="keyword">import</span> TitleOpts, VisualMapOpts</span><br><span class="line"></span><br><span class="line">province_data = <span class="literal">None</span></span><br><span class="line">province_data_list = <span class="built_in">list</span>()</span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">&quot;G:/疫情.txt&quot;</span>,<span class="string">&#x27;r&#x27;</span>,encoding=<span class="string">&#x27;utf8&#x27;</span>) <span class="keyword">as</span> f:</span><br><span class="line">    province_data = f.read()</span><br><span class="line">    <span class="comment"># 转换成字典一定不要忘！ str可没字典的字符串索引</span></span><br><span class="line">    province_data = json.loads(province_data)</span><br><span class="line">    province_data = province_data[<span class="string">&#x27;areaTree&#x27;</span>][<span class="number">0</span>][<span class="string">&#x27;children&#x27;</span>]</span><br><span class="line">    <span class="keyword">for</span> data <span class="keyword">in</span> province_data:</span><br><span class="line">        province_data_list.append((<span class="string">f&quot;<span class="subst">&#123;data[<span class="string">&#x27;name&#x27;</span>]&#125;</span>省&quot;</span>,data[<span class="string">&#x27;total&#x27;</span>][<span class="string">&#x27;confirm&#x27;</span>]))</span><br><span class="line"></span><br><span class="line"><span class="built_in">map</span> = Map()</span><br><span class="line"><span class="built_in">map</span>.add(<span class="string">&#x27;各省份确诊人数&#x27;</span>,province_data_list,<span class="string">&#x27;china&#x27;</span>)</span><br><span class="line"><span class="built_in">map</span>.set_global_opts(</span><br><span class="line">    title_opts=TitleOpts(title=<span class="string">&quot;全国确诊图&quot;</span>,pos_left=<span class="string">&#x27;center&#x27;</span>,pos_bottom=<span class="string">&#x27;1%&#x27;</span>),</span><br><span class="line">    visualmap_opts= VisualMapOpts(</span><br><span class="line">        is_show=<span class="literal">True</span>,  <span class="comment"># 是否显示</span></span><br><span class="line">        is_piecewise=<span class="literal">True</span>, <span class="comment"># 是否分段</span></span><br><span class="line">        pieces=[</span><br><span class="line">            &#123;<span class="string">&#x27;min&#x27;</span>:<span class="number">1</span>,<span class="string">&#x27;max&#x27;</span>:<span class="number">99</span>,<span class="string">&#x27;label&#x27;</span>:<span class="string">&#x27;1-99人&#x27;</span>,<span class="string">&#x27;color&#x27;</span>:<span class="string">&#x27;#CCFFFF&#x27;</span>&#125;,</span><br><span class="line">            &#123;<span class="string">&#x27;min&#x27;</span>:<span class="number">100</span>,<span class="string">&quot;max&quot;</span>:<span class="number">999</span>,<span class="string">&#x27;label&#x27;</span>:<span class="string">&quot;100-999人&quot;</span>,<span class="string">&#x27;color&#x27;</span>:<span class="string">&#x27;#FFFF99&#x27;</span>&#125;,</span><br><span class="line">            &#123;<span class="string">&#x27;min&#x27;</span>:<span class="number">1000</span>,<span class="string">&#x27;max&#x27;</span>:<span class="number">4999</span>,<span class="string">&#x27;label&#x27;</span>:<span class="string">&#x27;1000-4999人&#x27;</span>,<span class="string">&#x27;color&#x27;</span>:<span class="string">&#x27;#FF9966&#x27;</span>&#125;,</span><br><span class="line">            &#123;<span class="string">&#x27;min&#x27;</span>:<span class="number">5000</span>,<span class="string">&#x27;max&#x27;</span>:<span class="number">9999</span>,<span class="string">&#x27;label&#x27;</span>:<span class="string">&#x27;5000-9999人&#x27;</span>,<span class="string">&#x27;color&#x27;</span>:<span class="string">&#x27;#FF6666&#x27;</span>&#125;,</span><br><span class="line">            &#123;<span class="string">&#x27;min&#x27;</span>:<span class="number">10000</span>,<span class="string">&#x27;max&#x27;</span>:<span class="number">99999</span>,<span class="string">&#x27;label&#x27;</span>:<span class="string">&#x27;10000-99999人&#x27;</span>,<span class="string">&#x27;color&#x27;</span>:<span class="string">&#x27;#CC3333&#x27;</span>&#125;,</span><br><span class="line">            &#123;<span class="string">&#x27;min&#x27;</span>:<span class="number">100000</span>,<span class="string">&#x27;label&#x27;</span>:<span class="string">&#x27;100000+&#x27;</span>,<span class="string">&#x27;color&#x27;</span>:<span class="string">&#x27;#990033&#x27;</span>&#125;</span><br><span class="line">        ]</span><br><span class="line">    )</span><br><span class="line">)</span><br><span class="line"><span class="built_in">map</span>.render(<span class="string">&#x27;全国确诊地图.html&#x27;</span>) <span class="comment"># 控制生成文件名</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="3-柱状图的使用"><a href="#3-柱状图的使用" class="headerlink" title="3.柱状图的使用"></a>3.柱状图的使用</h3><h4 id="简单柱状图"><a href="#简单柱状图" class="headerlink" title="简单柱状图"></a>简单柱状图</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> pyecharts.charts <span class="keyword">import</span> Bar</span><br><span class="line"></span><br><span class="line">bar = Bar()</span><br><span class="line">bar.add_xaxis([<span class="string">&#x27;中国&#x27;</span>,<span class="string">&#x27;美国&#x27;</span>,<span class="string">&#x27;英国&#x27;</span>])</span><br><span class="line">bar.add_yaxis(<span class="string">&quot;GDP对比&quot;</span>,[<span class="number">35</span>,<span class="number">60</span>,<span class="number">25</span>])</span><br><span class="line">bar.reversal_axis() <span class="comment"># 把坐标系反转</span></span><br><span class="line">bar.render(<span class="string">&#x27;简单的柱状图生成.html&#x27;</span>)</span><br></pre></td></tr></table></figure>

<h4 id="1960-2019全球经济前十柱状图"><a href="#1960-2019全球经济前十柱状图" class="headerlink" title="1960-2019全球经济前十柱状图"></a>1960-2019全球经济前十柱状图</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> pyecharts.charts <span class="keyword">import</span> Bar,Timeline</span><br><span class="line"><span class="keyword">from</span> pyecharts.options <span class="keyword">import</span> LabelOpts,TitleOpts</span><br><span class="line"><span class="keyword">from</span> pyecharts.<span class="built_in">globals</span> <span class="keyword">import</span> ThemeType</span><br><span class="line"></span><br><span class="line"><span class="comment"># 设置主题</span></span><br><span class="line">timeline = Timeline(&#123;<span class="string">&#x27;theme&#x27;</span>: ThemeType.LIGHT&#125;)</span><br><span class="line">country_gdp_dict = &#123;&#125;</span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">&#x27;G:/1960-2019全球GDP数据.csv&#x27;</span>, <span class="string">&#x27;r&#x27;</span>, encoding=<span class="string">&#x27;gbk&#x27;</span>) <span class="keyword">as</span> f:</span><br><span class="line">    all_data = f.readlines()</span><br><span class="line">    all_data.pop(<span class="number">0</span>)</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> all_data:</span><br><span class="line">        data = i.split(<span class="string">&#x27;,&#x27;</span>)</span><br><span class="line">        year = data[<span class="number">0</span>]</span><br><span class="line">        country = data[<span class="number">1</span>]</span><br><span class="line">        <span class="comment"># 把科学计数法E用浮点数转换成数字</span></span><br><span class="line">        gdp = <span class="built_in">float</span>(data[<span class="number">2</span>])</span><br><span class="line">        <span class="comment"># 如果有 列表 则添加数据 如果 报错 无列表 则 初始化列表 再添加</span></span><br><span class="line">        <span class="keyword">try</span>:</span><br><span class="line">            country_gdp_dict[year].append([country, gdp])</span><br><span class="line">        <span class="keyword">except</span> KeyError:</span><br><span class="line">            country_gdp_dict[year] = []</span><br><span class="line">            country_gdp_dict[year].append([country, gdp])</span><br><span class="line"><span class="comment"># 把字典的keys排序一下 sorted默认升序排</span></span><br><span class="line">sorted_year = <span class="built_in">sorted</span>(country_gdp_dict.keys())</span><br><span class="line"><span class="keyword">for</span> year <span class="keyword">in</span> sorted_year:</span><br><span class="line">    <span class="comment"># 把一年里的国家按GDP排名 进行排序</span></span><br><span class="line">    country_gdp_dict[year].sort(key=<span class="keyword">lambda</span> element: element[<span class="number">1</span>], reverse=<span class="literal">True</span>)</span><br><span class="line">    year_data = country_gdp_dict[year][:<span class="number">10</span>]</span><br><span class="line">    x_data = []</span><br><span class="line">    y_data = []</span><br><span class="line">    <span class="comment"># 对每个国家进行遍历</span></span><br><span class="line">    <span class="keyword">for</span> country_gdp <span class="keyword">in</span> year_data:</span><br><span class="line">        x_data.append(country_gdp[<span class="number">0</span>])</span><br><span class="line">        y_data.append(country_gdp[<span class="number">1</span>] / <span class="number">100000000</span>)</span><br><span class="line">    bar = Bar()</span><br><span class="line">    <span class="comment"># 把世界第一排上面 默认是排下面</span></span><br><span class="line">    x_data.reverse()</span><br><span class="line">    y_data.reverse()</span><br><span class="line">    bar.add_xaxis(x_data)</span><br><span class="line">    <span class="comment"># 把标记放右边</span></span><br><span class="line">    bar.add_yaxis(<span class="string">&#x27;GDP(亿)&#x27;</span>,y_data,label_opts=LabelOpts(position=<span class="string">&#x27;right&#x27;</span>))</span><br><span class="line">    <span class="comment"># 把直的柱状图变成横的</span></span><br><span class="line">    bar.reversal_axis()</span><br><span class="line">    bar.set_global_opts(</span><br><span class="line">        title_opts=TitleOpts(title=<span class="string">f&#x27;<span class="subst">&#123;year&#125;</span>年GDP全球前十数据&#x27;</span>)</span><br><span class="line">    )</span><br><span class="line">    timeline.add(bar,<span class="built_in">str</span>(year))</span><br><span class="line"><span class="comment"># 设置一下自动播放配置项</span></span><br><span class="line">timeline.add_schema(</span><br><span class="line">    play_interval=<span class="number">1000</span>,</span><br><span class="line">    is_timeline_show=<span class="literal">True</span>,</span><br><span class="line">    is_auto_play=<span class="literal">True</span>,</span><br><span class="line">    is_loop_play=<span class="literal">False</span></span><br><span class="line">)</span><br><span class="line">timeline.render(<span class="string">&quot;1960-2019全世界dgp前十国家动态对比.html&quot;</span>)</span><br></pre></td></tr></table></figure>

</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="https://52zja.top">zjaaa</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="https://52zja.top/2024/02/08/python%E5%AD%A6%E4%B9%A0/">https://52zja.top/2024/02/08/python%E5%AD%A6%E4%B9%A0/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://52zja.top" target="_blank">云起</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/python/">python</a></div><div class="post_share"><div class="social-share" data-image="/img/icon.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1.1.3/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1.1.3/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2024/02/10/ER%E5%9B%BE/" title="ER图"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">ER图</div></div></a></div><div class="next-post pull-right"><a href="/2024/02/06/sql%E5%AD%A6%E4%B9%A0/" title="sql学习"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">sql学习</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src= "" data-lazy-src="/img/icon.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">zjaaa</div><div class="author-info__description">分享知识，认识世界</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">10</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">19</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">3</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/44528zja"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/44528zja" target="_blank" title="Github"><i class="fab fa-github" style="color: #24292e;"></i></a><a class="social-icon" href="https://github.com/44528zja" target="_blank" title="Email"><i class="fas fa-envelope" style="color: #4a7dbe;"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">欢迎来到我的碎碎念与生活杂记</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#python%E5%9F%BA%E7%A1%80"><span class="toc-number">1.</span> <span class="toc-text">python基础</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-%E5%AD%97%E9%9D%A2%E9%87%8F"><span class="toc-number">1.1.</span> <span class="toc-text">1.字面量</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-%E6%B3%A8%E9%87%8A"><span class="toc-number">1.2.</span> <span class="toc-text">2.注释</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-%E5%8F%98%E9%87%8F"><span class="toc-number">1.3.</span> <span class="toc-text">3.变量</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9F%A5%E7%9C%8B%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><span class="toc-number">1.3.1.</span> <span class="toc-text">查看数据类型</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E8%BD%AC%E6%8D%A2%E7%B1%BB%E5%9E%8B"><span class="toc-number">1.3.2.</span> <span class="toc-text">数据转换类型</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-%E6%A0%87%E8%AF%86%E7%AC%A6"><span class="toc-number">1.4.</span> <span class="toc-text">4.标识符</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-number">1.5.</span> <span class="toc-text">5.运算符</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6-%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E4%B8%89%E7%A7%8D%E5%AE%9A%E4%B9%89"><span class="toc-number">1.6.</span> <span class="toc-text">6.字符串的三种定义</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#7-%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%A0%BC%E5%BC%8F%E5%8C%96"><span class="toc-number">1.7.</span> <span class="toc-text">7.字符串格式化</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8F%AF%E4%BB%A5%E7%94%A8%E8%BE%85%E5%8A%A9%E7%AC%A6%E5%8F%B7m-n%E6%8E%A7%E5%88%B6%E6%95%B0%E6%8D%AE%E5%AE%BD%E5%BA%A6%E5%92%8C%E7%B2%BE%E5%BA%A6"><span class="toc-number">1.7.1.</span> <span class="toc-text">可以用辅助符号m.n控制数据宽度和精度</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%A0%BC%E5%BC%8F%E5%8C%96%E6%96%B9%E6%B3%952"><span class="toc-number">1.7.2.</span> <span class="toc-text">字符串格式化方法2</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%A1%A8%E8%BE%BE%E5%BC%8F%E6%A0%BC%E5%BC%8F%E5%8C%96"><span class="toc-number">1.7.3.</span> <span class="toc-text">表达式格式化</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#8-%E6%95%B0%E6%8D%AE%E8%BE%93%E5%85%A5"><span class="toc-number">1.8.</span> <span class="toc-text">8.数据输入</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#9-%E5%B8%83%E5%B0%94%E7%B1%BB%E5%9E%8B%E4%BB%A5%E5%8F%8A%E6%AF%94%E8%BE%83%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-number">1.9.</span> <span class="toc-text">9.布尔类型以及比较运算符</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#10-while%E5%BE%AA%E7%8E%AF"><span class="toc-number">1.10.</span> <span class="toc-text">10. while循环</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#while%E5%B5%8C%E5%A5%97%E5%BE%AA%E7%8E%AF"><span class="toc-number">1.10.1.</span> <span class="toc-text">while嵌套循环</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#11-for-%E5%BE%AA%E7%8E%AF"><span class="toc-number">1.11.</span> <span class="toc-text">11. for 循环</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#range%E5%BA%8F%E5%88%97"><span class="toc-number">1.11.1.</span> <span class="toc-text">range序列</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#continue"><span class="toc-number">1.11.2.</span> <span class="toc-text">continue</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#break"><span class="toc-number">1.11.3.</span> <span class="toc-text">break</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BE%AA%E7%8E%AF%E5%8F%91%E5%B7%A5%E8%B5%84%E5%B0%8F%E6%A1%88%E4%BE%8B"><span class="toc-number">1.11.4.</span> <span class="toc-text">循环发工资小案例</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#12-%E5%87%BD%E6%95%B0"><span class="toc-number">1.12.</span> <span class="toc-text">12.函数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#13-list%E5%88%97%E8%A1%A8"><span class="toc-number">1.13.</span> <span class="toc-text">13. list列表</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#list%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95"><span class="toc-number">1.13.1.</span> <span class="toc-text">list常用方法</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#14-%E5%85%83%E7%BB%84"><span class="toc-number">1.14.</span> <span class="toc-text">14.元组</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#15-%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-number">1.15.</span> <span class="toc-text">15.字符串</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#16-%E5%BA%8F%E5%88%97%E5%88%87%E7%89%87"><span class="toc-number">1.16.</span> <span class="toc-text">16.序列切片</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#17-set%E9%9B%86%E5%90%88"><span class="toc-number">1.17.</span> <span class="toc-text">17. set集合</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#18-%E5%AD%97%E5%85%B8"><span class="toc-number">1.18.</span> <span class="toc-text">18.字典</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#19-%E5%AE%B9%E5%99%A8%E9%80%9A%E7%94%A8%E6%96%B9%E6%B3%95"><span class="toc-number">1.19.</span> <span class="toc-text">19.容器通用方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#20-%E5%87%BD%E6%95%B0%E8%BF%9B%E9%98%B6"><span class="toc-number">1.20.</span> <span class="toc-text">20.函数进阶</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#21-%E6%96%87%E4%BB%B6"><span class="toc-number">1.21.</span> <span class="toc-text">21.文件</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E6%89%93%E5%BC%80%E6%96%87%E4%BB%B6"><span class="toc-number">1.21.1.</span> <span class="toc-text">1.打开文件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E8%AF%BB%E5%85%A5%E6%96%87%E4%BB%B6"><span class="toc-number">1.21.2.</span> <span class="toc-text">2.读入文件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-%E5%86%99%E5%85%A5%E6%96%87%E4%BB%B6"><span class="toc-number">1.21.3.</span> <span class="toc-text">3.写入文件</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#22-%E5%BC%82%E5%B8%B8"><span class="toc-number">1.22.</span> <span class="toc-text">22.异常</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#23-%E6%A8%A1%E5%9D%97"><span class="toc-number">1.23.</span> <span class="toc-text">23.模块</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%87%AA%E5%AE%9A%E4%B9%89%E6%A8%A1%E5%9D%97"><span class="toc-number">1.23.1.</span> <span class="toc-text">自定义模块</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#24-python%E5%8C%85"><span class="toc-number">1.24.</span> <span class="toc-text">24. python包</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%87%AA%E5%B7%B1%E5%86%99%E4%B8%80%E4%B8%AApython%E5%8C%85"><span class="toc-number">1.24.1.</span> <span class="toc-text">自己写一个python包</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#25-json"><span class="toc-number">1.25.</span> <span class="toc-text">25. json</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#python%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1"><span class="toc-number">2.</span> <span class="toc-text">python面向对象</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-%E7%B1%BB"><span class="toc-number">2.1.</span> <span class="toc-text">1.类</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-%E6%9E%84%E9%80%A0%E6%96%B9%E6%B3%95"><span class="toc-number">2.2.</span> <span class="toc-text">2.构造方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-%E9%AD%94%E6%9C%AF%E6%96%B9%E6%B3%95"><span class="toc-number">2.3.</span> <span class="toc-text">3.魔术方法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B8%B8%E8%A7%81%E6%96%B9%E6%B3%95"><span class="toc-number">2.3.1.</span> <span class="toc-text">常见方法</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-%E5%B0%81%E8%A3%85"><span class="toc-number">2.4.</span> <span class="toc-text">4.封装</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%A4%8D%E5%86%99-%E7%88%B6%E7%B1%BB%E6%96%B9%E6%B3%95%E5%B1%9E%E6%80%A7"><span class="toc-number">2.4.1.</span> <span class="toc-text">复写 父类方法属性</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E8%A2%AB%E5%AD%90%E7%B1%BB%E9%87%8D%E5%86%99%E7%9A%84%E7%88%B6%E7%B1%BB%E6%96%B9%E6%B3%95%E5%92%8C%E5%8F%98%E9%87%8F"><span class="toc-number">2.4.1.1.</span> <span class="toc-text">使用被子类重写的父类方法和变量</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6-%E7%B1%BB%E5%9E%8B%E6%B3%A8%E8%A7%A3"><span class="toc-number">2.5.</span> <span class="toc-text">6.类型注解</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6-%E5%A4%9A%E6%80%81"><span class="toc-number">2.6.</span> <span class="toc-text">6.多态</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#7-python%E4%BD%BF%E7%94%A8mysql"><span class="toc-number">2.7.</span> <span class="toc-text">7. python使用mysql</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#8-%E9%97%AD%E5%8C%85"><span class="toc-number">2.8.</span> <span class="toc-text">8.闭包</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#9-%E8%A3%85%E9%A5%B0%E5%99%A8"><span class="toc-number">2.9.</span> <span class="toc-text">9.装饰器</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#10-%E5%A4%9A%E7%BA%BF%E7%A8%8B"><span class="toc-number">2.10.</span> <span class="toc-text">10.多线程</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%AC%AC%E4%B8%89%E6%96%B9%E5%8C%85%E7%9A%84%E4%BD%BF%E7%94%A8%E4%BB%A5%E5%8F%8A%E8%AE%B0%E5%BD%95"><span class="toc-number">3.</span> <span class="toc-text">第三方包的使用以及记录</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#pyechars%E5%9B%BE%E4%BE%8B%E5%BA%93%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-number">3.1.</span> <span class="toc-text">pyechars图例库的使用</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E6%8A%98%E7%BA%BF%E5%9B%BE%E4%BD%BF%E7%94%A8"><span class="toc-number">3.1.1.</span> <span class="toc-text">1.折线图使用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E5%9C%B0%E5%9B%BE%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-number">3.1.2.</span> <span class="toc-text">2.地图的使用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-%E6%9F%B1%E7%8A%B6%E5%9B%BE%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-number">3.1.3.</span> <span class="toc-text">3.柱状图的使用</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%AE%80%E5%8D%95%E6%9F%B1%E7%8A%B6%E5%9B%BE"><span class="toc-number">3.1.3.1.</span> <span class="toc-text">简单柱状图</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1960-2019%E5%85%A8%E7%90%83%E7%BB%8F%E6%B5%8E%E5%89%8D%E5%8D%81%E6%9F%B1%E7%8A%B6%E5%9B%BE"><span class="toc-number">3.1.3.2.</span> <span class="toc-text">1960-2019全球经济前十柱状图</span></a></li></ol></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/02/13/%E8%8B%B1%E6%96%87%E5%8D%95%E8%AF%8D%E5%AD%A6%E4%B9%A0/" title="英文单词学习">英文单词学习</a><time datetime="2024-02-13T05:10:38.000Z" title="发表于 2024-02-13 13:10:38">2024-02-13</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/02/10/html%E4%BB%A5%E5%8F%8Acss%E5%AD%A6%E4%B9%A0/" title="html以及css学习">html以及css学习</a><time datetime="2024-02-10T13:35:11.000Z" title="发表于 2024-02-10 21:35:11">2024-02-10</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/02/10/vue%E5%AD%A6%E4%B9%A0/" title="vue学习">vue学习</a><time datetime="2024-02-10T08:35:56.000Z" title="发表于 2024-02-10 16:35:56">2024-02-10</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/02/10/ER%E5%9B%BE/" title="ER图">ER图</a><time datetime="2024-02-10T03:40:24.000Z" title="发表于 2024-02-10 11:40:24">2024-02-10</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/02/08/python%E5%AD%A6%E4%B9%A0/" title="python学习">python学习</a><time datetime="2024-02-08T04:11:12.000Z" title="发表于 2024-02-08 12:11:12">2024-02-08</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2024 By zjaaa</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js?v=4.13.0"></script><script src="/js/main.js?v=4.13.0"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui@5.0.33/dist/fancybox/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload@17.8.8/dist/lazyload.iife.min.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>